package jmind.core.serialize;

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.io.IOException;

/**
 * description:   Protostuff
 *   处理list 这种类型有问题
 *   https://www.jianshu.com/p/620aaa93e492
 * @author weibo.xie
 * @date : create in 3:05 下午 2021/5/14
 */
public class ProtostuffSerializer extends ActualType implements  Serializer{

    //  针对list，set，map等类可以包装一下
    private boolean isWarp;

    public ProtostuffSerializer(Class type) {
        this(type,false);
    }

    public ProtostuffSerializer(Class type,boolean isWarp) {
        super(type);
        this.isWarp=isWarp;
    }

    @Override
    public byte[] serialize(Object object) throws IOException {
        LinkedBuffer buffer = LinkedBuffer.allocate();
        Schema schema = getSchema();
        byte[] data;
        try {
            if(isWarp){
                object= SerializeWrapper.builder(object);
            }
            data = ProtostuffIOUtil.toByteArray(object, schema, buffer);
        } finally {
            buffer.clear();
        }

        return data;
    }

    @Override
    public Object deserialize(byte[] bytes) throws Exception {
        Schema schema = getSchema();
        Object obj = schema.newMessage();
        ProtostuffIOUtil.mergeFrom(bytes, obj, schema);
        if(isWarp){
           return  ((SerializeWrapper)obj).getData();
        }
        return obj;
    }

    private Schema  getSchema(){
        Class clazz=isWarp? SerializeWrapper.class:getType();
        return RuntimeSchema.getSchema(clazz);
    }



    //静态内部类，可以处理list，Map，set等
    public static class SerializeWrapper<T> {
        //泛型的使用
        private T data;

        //建造者模式(返回实体类型)
        public static <T> SerializeWrapper<T> builder(T data) {
            SerializeWrapper<T> wrapper = new SerializeWrapper<T>();
            wrapper.setData(data);
            return wrapper;
        }

        public void setData(T data) {
            this.data = data;
        }

        public T getData() {
            return data;
        }
    }

       /******序列化List********/


//    public  <T> byte[] serializeList(List<T> objList) throws IOException {
//        if (objList == null || objList.isEmpty()) {
//            throw new RuntimeException("序列化对象列表(" + objList + ")参数异常!");
//        }
//        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(objList.get(0).getClass());
//        LinkedBuffer buffer = LinkedBuffer.allocate();
//        byte[] protostuff = null;
//        ByteArrayOutputStream bos = null;
//        try {
//            bos = new ByteArrayOutputStream();
//            ProtostuffIOUtil.writeListTo(bos, objList, schema, buffer);
//            protostuff = bos.toByteArray();
//
//        } finally {
//            buffer.clear();
//            try {
//                if(bos!=null){
//                    bos.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//        return protostuff;
//    }
//
//    public  <T> List<T> deserializeList(byte[] paramArrayOfByte, Class<T> targetClass) throws IOException {
//        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {
//            throw new RuntimeException("反序列化对象发生异常,byte序列为空!");
//        }
//        Schema<T> schema = RuntimeSchema.getSchema(targetClass);
//        List<T> result =ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema);
//
//        return result;
//    }

}